package com.opengltest;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;

import com.defence.R;
import com.game.utils.ImageTool;

/**
 * @author oven:
 * @version 创建时间：2013-1-26 下午5:10:59
 * 
 */
public class SixcornTexture {
	
	
	// 保存纹理的FloatBuffer  
	  
    private FloatBuffer[] textureBuffer;  
  
    // 用来加载纹理的数组  
    private int[] textures = new int[8];  
  
    // 保存纹理顶点坐标的数组  
  
    private float[][] texture = {  
            new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f, 1.0f, 0.5f, 0.0f,  
                    1.0f, 1.0f, 1.0f,  
  
            },  
            new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f, 1.0f, 0.5f, 0.0f,  
                    1.0f, 1.0f, 1.0f,  
  
            }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,  
  
            }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,  
  
            }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,  
  
            }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,  
  
            }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,  
  
            }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,  
  
            } };  
    
	 // 保存每一个平面的顶点索引的ByteBuffer数组  
    private ByteBuffer[] indexBuffer;  
  
    // 保存每一个平面的顶点坐标的FloatBuffer数组  
    private FloatBuffer[] faceVertexBuffer;  
  
    // 保存每一个平面的顶点坐标的二维数组  
  
    private float[][] faceVertices = { new float[] { -0.5f, -0.816f, 1.0f,// bottom  
                                                                            // left  
                                                                            // 0  
            0.5f, -0.816f, 1.0f,// bottom right 1  
            -1.0f, 0.0f, 1.0f,// middle left 2  
            1.0f, 0.0f, 1.0f,// middle right 3  
            -0.5f, 0.816f, 1.0f, // top left 4  
            0.5f, 0.816f, 1.0f // top right 5  
            },// front face  
            new float[] { -0.5f, -0.816f, -1.0f,// bottom left 6  
                    0.5f, -0.816f, -1.0f,// bottom right 7  
                    -1.0f, 0.0f, -1.0f,// middle left 8  
                    1.0f, 0.0f, -1.0f,// middle right 9  
                    -0.5f, 0.816f, -1.0f, // top left 10  
                    0.5f, 0.816f, -1.0f // top right 11  
            },// back face  
            new float[] { -0.5f, -0.816f, -1.0f,// bottom left 6  
                    0.5f, -0.816f, -1.0f,// bottom right 7  
                    -0.5f, -0.816f, 1.0f,// bottom left 0  
                    0.5f, -0.816f, 1.0f // bottom right 1  
            },// bottom face  
            new float[] { -1.0f, 0.0f, -1.0f,// middle left 8  
                    -0.5f, -0.816f, -1.0f,// bottom left 6  
                    -1.0f, 0.0f, 1.0f,// middle left 2  
                    -0.5f, -0.816f, 1.0f // bottom left 0  
            },// bottom left face  
            new float[] { 0.5f, -0.816f, -1.0f,// bottom right 7  
                    1.0f, 0.0f, -1.0f,// middle right 9  
                    0.5f, -0.816f, 1.0f,// bottom right 1  
                    1.0f, 0.0f, 1.0f // middle right 3  
            },// bottom right face  
            new float[] { 0.5f, 0.816f, -1.0f, // top right 11  
                    1.0f, 0.0f, -1.0f,// middle right 9  
                    0.5f, 0.816f, 1.0f,// top right 5  
                    1.0f, 0.0f, 1.0f // middle right 3  
            },// top right face  
            new float[] { -0.5f, 0.816f, -1.0f, // top left 10  
                    0.5f, 0.816f, -1.0f, // top right 11  
                    -0.5f, 0.816f, 1.0f,// top left 4  
                    0.5f, 0.816f, 1.0f // top right 5  
            },// top face  
            new float[] { -0.5f, 0.816f, -1.0f, // top left 10  
                    -1.0f, 0.0f, -1.0f,// middle left 8  
                    -0.5f, 0.816f, 1.0f, // top left 4  
                    -1.0f, 0.0f, 1.0f // middle left 2  
            } // top left face  
    };  
  
    // 定义每一个平面颜色的数组  
  
    private float[] colors = { 1.0f, 0.0f, 0.0f, 1.0f, // 0  
            0.0f, 1.0f, 0.0f, 1.0f,// 1  
            0.0f, 0.0f, 1.0f, 1.0f, // 2  
            1.0f, 1.0f, 1.0f, 1.0f, // 3  
            1.0f, 1.0f, 0.0f, 1.0f,// 4  
            1.0f, 0.0f, 1.0f, 1.0f,// 5  
            0.0f, 1.0f, 1.0f, 1.0f,// 6  
            0.5f, 0.5f, 0.5f, 1.0f, // 7  
    };  
  
    // 保存每一个平面的顶点索引的二维数组  
  
    private byte[][] indies = {  
            new byte[] { 0, 1, 2, 1, 3, 2, 2, 3, 4, 3, 5, 4 },// front face  
            new byte[] { 0, 1, 2, 1, 3, 2, 2, 3, 4, 3, 5, 4 },// back face  
            new byte[] { 0, 1, 2, 1, 3, 2 },// bottom face  
            new byte[] { 0, 1, 2, 1, 3, 2 },// bottom left face  
            new byte[] { 0, 1, 2, 1, 3, 2 },// bottom right face  
            new byte[] { 0, 1, 2, 1, 3, 2 },// top right face  
            new byte[] { 0, 1, 2, 1, 3, 2 },// top face  
            new byte[] { 0, 1, 2, 1, 3, 2 } // top left face  
    };  
  
    public SixcornTexture() {  
  
        // 利用循环初始化顶点坐标faceVertexBuffer数组和顶点索引indexBuffer数组  
        ByteBuffer bb;  
        faceVertexBuffer = new FloatBuffer[8];  
        for (int i = 0; i < faceVertices.length; i++) {  
            bb = ByteBuffer.allocateDirect(faceVertices[i].length * 4);  
            bb.order(ByteOrder.nativeOrder());  
            faceVertexBuffer[i] = bb.asFloatBuffer();  
            faceVertexBuffer[i].put(faceVertices[i]);  
            faceVertexBuffer[i].position(0);  
  
        }  
  
        indexBuffer = new ByteBuffer[8];  
        for (int i = 0; i < indies.length; i++) {  
            indexBuffer[i] = ByteBuffer.allocateDirect(indies[i].length);  
            indexBuffer[i].put(indies[i]);  
            indexBuffer[i].position(0);  
  
        }  
        
        textureBuffer = new FloatBuffer[8];  
        for (int i = 0; i < 8; i++) {  
            bb = ByteBuffer.allocateDirect(texture[i].length * 4);  
            bb.order(ByteOrder.nativeOrder());  
            textureBuffer[i] = bb.asFloatBuffer();  
            textureBuffer[i].put(texture[i]);  
            textureBuffer[i].position(0);  
        }  
    }  
  
    public void draw(GL10 gl) {  
  
        // 利用循环绘制六棱柱的每一个面，并给不同的面设置不同的颜色  
        gl.glFrontFace(GL10.GL_CW);  
  
        for (int i = 0; i < 8; i++) {  
            // Generate one texture pointer...  
            // ...and bind it to our array  
  
            // 生成一个纹理引用，并把它和当前的数组绑定  
            gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[i]);  
            // Point to our buffers  
            // 设置纹理坐标  
  
            gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer[i]);  
            // Enable the texture state  
            // 加入纹理坐标的权限  
  
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);  
            // Disable the client state before leaving  
            // 取消纹理坐标的权限  
  
            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);  
        }  
    }  
    
    
 // 加载和绑定纹理的方法  
    public void loadTexture(GL10 gl) {  
        gl.glGenTextures(8, textures, 0);  
        // Get the texture from the Android resource directory  
        for (int i = 0; i < 8; i++) {  
            Bitmap bitmap = ImageTool.getBitMap("p1.jpg");
            // Generate one texture pointer...  
            // ...and bind it to our array  
            gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[i]);  
  
            // Create Nearest Filtered Texture  
            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,  
                    GL10.GL_NEAREST);  
            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,  
                    GL10.GL_LINEAR);  
            // Use the Android GLUtils to specify a two-dimensional texture  
            // image from our bitmap  
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);  
            // Clean up  
            bitmap.recycle();  
        }  
    }  
  
    // 加载bitmap  
  
    public Bitmap loadBitmap(Context context, int resourceid) {  
  
        InputStream is = context.getResources().openRawResource(resourceid);  
        try {  
            // BitmapFactory is an Android graphics utility for images  
            return BitmapFactory.decodeStream(is);  
  
        } finally {  
            // Always clear and close  
            try {  
                is.close();  
                is = null;  
            } catch (IOException e) {  
            }  
        }  
    }  
}

